//
//  ZCOriginateView.swift
//  weibo
//
//  Created by 张城 on 16/7/23.
//  Copyright © 2016年 张城. All rights reserved.
//

import UIKit
import SnapKit

class ZCOriginateView: UIView {
    
    var orginalViewBottomConstraint: Constraint?
    
    
    
    var homeCellViewModel: ZCHomeCellViewModel? {
        didSet{
            self.headImg.zc_setImg((homeCellViewModel?.homeModel?.user?.profile_image_url)!, holdImgName: "avatar_default")
            
            self.nameLabel.text = homeCellViewModel?.homeModel?.user?.name
            
            self.vipImg.image = homeCellViewModel?.vipImage
            
            self.avatarImg.image = homeCellViewModel?.avatarImage
            
            self.timeLabel.text = homeCellViewModel?.timeLabeltext
            
            self.sourceLabel.attributedText = homeCellViewModel?.sourceAttr
            
            
            
            
            
            
            
            self.textLabel.text = homeCellViewModel?.homeModel?.text
//            print(self.textLabel.text)
            
            
            self.orginalViewBottomConstraint?.uninstall()
            
            if homeCellViewModel?.homeModel?.pic_urls?.count > 0 {
                
                picturesView.pictures = homeCellViewModel?.homeModel?.pic_urls
                
                self.snp_makeConstraints(closure: { (make) in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(picturesView.snp_bottom).offset(10).constraint
                })
                picturesView.hidden = false
                
            }else{
                self.snp_makeConstraints(closure: { (make) in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(10).constraint
                })
                picturesView.hidden = true
                
                
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(headImg)
        addSubview(nameLabel)
        addSubview(vipImg)
        addSubview(avatarImg)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(textLabel)
        addSubview(picturesView)
        
        headImg.snp_makeConstraints { (make) in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImg)
            make.leading.equalTo(headImg.snp_trailing).offset(10)
        }
        
        vipImg.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
        }
        
        avatarImg.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImg.snp_trailing)
            make.centerY.equalTo(headImg.snp_bottom)
        }
        
        timeLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImg.snp_bottom)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
        }
        
        textLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImg.snp_bottom).offset(15)
            make.leading.equalTo(headImg)
        }
        
        picturesView.snp_makeConstraints { (make) in
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW-250)/2)
//            make.size.equalTo(CGSizeMake(80, 80))
        }
        
        self.snp_makeConstraints { (make) in
            self.orginalViewBottomConstraint = make.bottom.equalTo(picturesView.snp_bottom).offset(10).constraint
        }
    }

    
    
    private lazy var headImg: UIImageView = {
        let head = UIImageView(image: UIImage(named:"avatar_default"))
        head.layer.cornerRadius = 17
        head.layer.masksToBounds = true
        return head
    }()
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.redColor(), textFont: 15)
    private lazy var vipImg: UIImageView = UIImageView(image: UIImage(named:"common_icon_membership"))
    private lazy var avatarImg: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), textFont: 12)
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), textFont: 12)
    
    private lazy var textLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWidth: SCREENW-40)
    
    private lazy var picturesView: ZCPicturesView = ZCPicturesView()
    
    
}
